package com.mdd.admin.task;

import com.github.yulichang.query.MPJQueryWrapper;
import com.mdd.common.config.GlobalConfig;
import com.mdd.common.entity.album.Album;
import com.mdd.common.entity.course.Course;
import com.mdd.common.entity.course.CourseCatalogue;
import com.mdd.common.mapper.album.AlbumMapper;
import com.mdd.common.mapper.course.CourseCatalogueMapper;
import com.mdd.common.utils.YmlUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import ws.schild.jave.EncoderException;
import ws.schild.jave.MultimediaInfo;
import ws.schild.jave.MultimediaObject;

import javax.annotation.Resource;
import java.io.File;
import java.util.Arrays;
import java.util.List;

@Component
public class FileUpdateDurationTask {

    private static final Logger logger = LoggerFactory.getLogger(FileUpdateDurationTask.class);
    @Resource
    CourseCatalogueMapper courseCatalogueMapper;

    @Resource
    AlbumMapper albumMapper;

    //每天凌晨1点执行
    @Scheduled(cron = "0 0 1 * * ?")
    public void update() throws EncoderException {
        MPJQueryWrapper<CourseCatalogue> mpjCourseCatalogueQueryWrapper = new MPJQueryWrapper<CourseCatalogue>()
                .selectAll(CourseCatalogue.class)
                .innerJoin("?_course course ON t.course_id = course.id".replace("?_", GlobalConfig.knowledgePaymentTablePrefixe))
                .in("course.type", Arrays.asList(2, 3))
                .isNull("t.duration");
        List<CourseCatalogue> courseCatalogues = courseCatalogueMapper.selectJoinList(CourseCatalogue.class, mpjCourseCatalogueQueryWrapper);

        String directory = YmlUtil.get("like.upload-directory");
        if (directory == null || directory.equals("")) {
            directory = GlobalConfig.uploadDirectory;
        }

        logger.info("更新课程目录时长");
        for (CourseCatalogue courseCatalogue : courseCatalogues) {
            File tempFile = new File(directory + courseCatalogue.getContent());

            if (tempFile.isFile() && tempFile.exists()) {
                MultimediaObject instance = new MultimediaObject(tempFile);
                MultimediaInfo multimediaInfo = instance.getInfo();
                long duration = multimediaInfo.getDuration();
                courseCatalogue.setDuration(duration);
                System.out.println(courseCatalogues);
                this.courseCatalogueMapper.updateById(courseCatalogue);
            }
        }


        MPJQueryWrapper<Album> albumQueryWrapper = new MPJQueryWrapper<Album>()
                .selectAll(Album.class)
                .in("t.type", Arrays.asList(20, 40))
                .eq("t.duration", 0);
        List<Album> albums = albumMapper.selectList(albumQueryWrapper);

        logger.info("更新资源时长");
        for (Album album : albums) {
            File tempFile = new File(directory + album.getUri());

            if (tempFile.isFile() && tempFile.exists()) {
                MultimediaObject instance = new MultimediaObject(tempFile);
                MultimediaInfo multimediaInfo = instance.getInfo();
                long duration = multimediaInfo.getDuration();
                album.setDuration(duration);
                System.out.println(courseCatalogues);
                this.albumMapper.updateById(album);
            }
        }

    }

}
